<?php

namespace App\Http\Controllers\Steel;

use App\Http\Controllers\BaseController;
use App\Models\Manger\Mangerrule;
use App\Models\Steel\SteelAttrType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

/**
 * 钢材属性分类相关
 */
class SteelAttrTypeController extends BaseController
{

    protected $attrtomenu = [];
    /**
     * 获取自定义属性分类列表
     */
    public function getlist(Request $request)
    {
        $list = SteelAttrType::where(['cpid' => $this->mangerinfo['cpid']])->select(['id', 'cpid', 'typename', 'status'])->get();
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
    }

    /**
     * 增加新属性分类，需要给管理员自己添加权限菜单
     */
    public function dopostadd(Request $request)
    {

        if ($this->mangerinfo['utype'] != 1) {
            return response()->json(['code' => '001', 'msg' => '无权限']);
        }
        $typename = $request->typename;
        if ($typename == '') {
            return response()->json(['code' => '001', 'msg' => '菜单名称不能为空']);
        }
        $isexist = SteelAttrType::where([['typename', $typename], ['cpid', $this->mangerinfo['cpid']]])->orWhere([['typename', $typename], ['cpid', 0]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '菜单名称不允许重复']);
        }
        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'typename' => $typename,
            'update_at' => time()
        ];
        $data['create_at'] = time();
        SteelAttrType::create($data)->id;

        //添加菜单权限,无需添加，获取菜单时添加






        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 冻结属性，需要删除管理员权限
     */
    public function changestatus(Request $request)
    {

        $id = $request->id ?? 0;
        $info = SteelAttrType::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        SteelAttrType::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        //删除菜单权限

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
